/** 
  Copyright (c) 2008, 2009 Claudio Imbrenda
  This program is released under the terms of the
  GNU GPL version 3 or later.
*/
package macchina.nastri;

import java.io.*;
import macchina.*;

public class ASCIIFileNastro implements AdattatoreEntrata{

  FileReader inf;
  
  public ASCIIFileNastro(File f)throws FileNotFoundException{
    inf=new FileReader(f);
  }
  public ASCIIFileNastro(String s) throws FileNotFoundException{
    inf=new FileReader(s);
  }
  
  
  private boolean ioerr=false;
  private boolean isloop=false;
  
  public byte read(){
    int rr;
    if(ioerr)
      return AdattatoreEntrata.EOT|AdattatoreEntrata.IOERR;
    
    try{
        rr = inf.read();
        if(rr==-1){
            if(isloop){
                inf.reset();
                rr=inf.read();
            }else
                return 32;
        }
    }
    catch(IOException e){ioerr=true;return read();}
    
    
    return MRUtils.toMR((char)rr);
  }

    public void setLoop(boolean v) {
        isloop=v;
    }

    public boolean isLoop() {
        return isloop;
    }

}
